package com.qf.config;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;

//切面类
@Aspect
//交给spring
@Component
public class LoggerAop {

    private Logger logger = LoggerFactory.getLogger(LoggerAop.class);

    //定义切点
    //第一个* 代表任意返回值
    //代表包明
    @Pointcut(value = "execution(* *.*.*.impl.*.*(..))")
    public void pt1(){
    }

    @Around(value = "pt1()")
    public Object arount(ProceedingJoinPoint proceedingJoinPoint){
        //1.首先获取到执行的方法
        MethodSignature signature =(MethodSignature) proceedingJoinPoint.getSignature();
        //2.获取到当前方法传递的参数
        Object[] args = proceedingJoinPoint.getArgs();
        Date date = new Date();
        String format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(date);

        logger.debug("当前方法进入时间：{}"+format);
        logger.debug("当前执行的方法：{}"+signature.getMethod());
        logger.debug("当前参数：{}"+ Arrays.toString(args));
        Object proceed=null;
        try {
            proceed = proceedingJoinPoint.proceed();
            logger.debug("返回的参数为：{}"+ signature.getReturnType());
            return proceed;
        } catch (Throwable throwable) {
            throwable.printStackTrace();
            logger.error("方法：{}"+ signature.getMethod()+"发生异常：{}"+throwable.getMessage());
        }finally {
            return proceed;
        }


    }
}
